<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">名称：</label>
                                    <div class="layui-input-inline">
                                        <input name="name" class="layui-input" type="text" placeholder="请输入名称"/>
                                    </div>
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="deviceTypeBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt">

                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="deviceTypeTable" lay-filter="deviceTypeTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="deviceTypeTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="deviceSpecBar">
    <a class="layui-btn layui-btn-xs" lay-event="addSpec">添加规格</a>
    <a class="layui-btn layui-btn-xs" lay-event="specTable">规格列表</a>
</script>

<script type="text/html" id="deviceAttrBar">
    <a class="layui-btn  layui-btn-xs" lay-event="addAttr">添加属性</a>
    <a class="layui-btn  layui-btn-xs" lay-event="attrTable">属性列表</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="deviceTypeForm">
    <form lay-filter="deviceTypeForm" class="layui-form model-form">
        <input name="id" type="hidden"/>

        <div class="layui-form-item">
            <label class="layui-form-label">类型名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入类型名称" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>

        <div class="layui-form-item" >
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <textarea id="remark" name="remark" placeholder="请输入描述" class="layui-textarea" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-block">
                <input name="isDeleted" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="启用|停用"/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input name="sortNumber" placeholder="请输入排序" type="text" class="layui-input" lay-verify="" required/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="deviceTypeFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<script type="text/html" id="deviceSpecForm">
    <form lay-filter="deviceSpecForm" class="layui-form model-form">
        <input name="id" type="hidden"/>

        <div class="layui-form-item">
            <label class="layui-form-label">设备类型</label>
            <div class="layui-input-block">
                <select  name="productTypeId" lay-filter="productTypeId" lay-search lay-verify="required" disabled>

                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">规格名称</label>
            <div class="layui-input-block">
                <input name="specName" placeholder="请输入规格名称" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item" >
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <textarea  name="remark" placeholder="请输入描述" class="layui-textarea" />
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input name="sortNumber" placeholder="请输入排序" type="text" class="layui-input" lay-verify="" required/>
            </div>
        </div>

        <div class="layui-form-item" id="btnAddDiv">
            <label class="layui-form-label"></label>
            <div class="layui-input-inline">
                <button id="btnAdd" class="layui-btn" lay-filter="btnAdd" onclick="return false;">添加规格值</button>
            </div>
        </div>

        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="deviceSpecFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<script type="text/html" id="deviceSpecTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="editSpec">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="deviceAttrTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="editAttr">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="specTable">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->
                <div class="layui-form toolbar">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label w-auto">名称：</label>
                            <div class="layui-input-inline mr0">
                                <input name="name" class="layui-input" type="text" placeholder="请输入名称"/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn icon-btn" lay-filter="formSubSearchLog" lay-submit><i class="layui-icon">&#xe615;</i>搜索</button>
                            <button id="productSpecBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                            <button id="productSpecBtnDel" class="layui-btn icon-btn layui-btn-danger"><i class="layui-icon"></i>批量删除</button>
                        </div>
                    </div>
                </div>
                <!-- 表格 -->
                <table class="layui-table" id="deviceSpecTable" lay-filter="deviceSpecTable"></table>
            </div>
        </div>
    </div>
</script>

<script type="text/html" id="deviceAttrForm">
    <form lay-filter="deviceAttrForm" class="layui-form model-form">
        <input name="id" type="hidden"/>

        <div class="layui-form-item">
            <label class="layui-form-label">设备类型</label>
            <div class="layui-input-block">
                <select  name="productTypeId" lay-filter="productTypeId" lay-search lay-verify="required" disabled>

                </select>
            </div>
        </div>

        <!--<div class="layui-form-item">
            <label class="layui-form-label">属性分类</label>
            <input name="categoryId" type="hidden"/>
            <div class="layui-input-block" id="selectCategory">
            </div>
        </div>-->

        <div class="layui-form-item">
            <label class="layui-form-label">属性名称</label>
            <div class="layui-input-block">
                <input name="attrName" placeholder="请输入名称" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>


        <!--<div class="layui-form-item">
            <label class="layui-form-label">属性头</label>
            <div class="layui-input-block">
                <input name="attrHeader" placeholder="请输入属性头" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>-->

        <div class="layui-form-item" >
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <textarea  name="remark" placeholder="请输入描述" class="layui-textarea" />
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-block">
                <input name="isDeleted" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="启用|停用"/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input name="sortNumber" placeholder="请输入排序" type="text" class="layui-input" lay-verify="" required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">属性类型</label>
            <div class="layui-input-block">
                <select name="style" lay-filter="style">
                    <option value="">请选择</option>
                    <option value="text">文本</option>
                    <option value="single">固定选择项</option>

                </select>
            </div>
        </div>

        <div class="layui-form-item" id="btnAddDiv2">
            <label class="layui-form-label"></label>
            <div class="layui-input-inline">
                <button id="btnAdd2" class="layui-btn" lay-filter="btnAdd2" onclick="return false;">添加选择项</button>
            </div>
        </div>

        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="deviceAttrFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>


<script type="text/html" id="attrTable">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->
                <div class="layui-form toolbar">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label w-auto">名称：</label>
                            <div class="layui-input-inline mr0">
                                <input name="name" class="layui-input" type="text" placeholder="请输入名称"/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn icon-btn" lay-filter="formSubSearchLog" lay-submit><i class="layui-icon">&#xe615;</i>搜索</button>
                            <button id="productAttrBtnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                            <button id="productAttrBtnDel" class="layui-btn icon-btn layui-btn-danger"><i class="layui-icon"></i>批量删除</button>
                        </div>
                    </div>
                </div>
                <!-- 表格 -->
                <table class="layui-table" id="deviceAttrTable" lay-filter="deviceAttrTable"></table>
            </div>
        </div>
    </div>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'laydate', 'admin' ,'laytpl','tableTreeDj', 'config'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laydate = layui.laydate;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laytpl = layui.laytpl;
        var tableTree = layui.tableTreeDj;

        // 渲染表格
        var insTb = table.render({
            elem: '#deviceTypeTable',
            url: config.base + 'enterprise/device/admin/deviceType/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'}
                ,{field: 'name', sort: true, title: '类型名称'}
                ,{field: 'code', sort: true, title: '类型编号'}
                ,{field: 'remark', sort: true, title: '描述'}
                ,{field: 'isDeleted', sort: true, title: '是否启用',templet:function (d) {
                    if(d.isDeleted){
                        return '<input name="isDeleted" type="checkbox" lay-skin="switch" value="'+d.id+'" checked="'+d.isDeleted+'" lay-filter="isDeleted"  lay-text="启用|停用"/>';
                    }else{
                        return '<input name="isDeleted" type="checkbox" lay-skin="switch" value="'+d.id+'" lay-filter="isDeleted"   lay-text="启用|停用"/>';
                    }

                    }}
                ,{field: 'createTime', sort: true, title: '创建时间'}
                ,{align: 'center', toolbar: '#deviceSpecBar', title: '规格管理', width: 200}
                ,{align: 'center', toolbar: '#deviceAttrBar', title: '属性管理', width: 200}
                ,{align: 'center', toolbar: '#deviceTypeTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if(menuButton.indexOf("搜索") == -1){
                    $('.layui-form.toolbar').remove();
                }
                if(menuButton.indexOf("修改") == -1){
                    $("[lay-event='edit']").remove();
                }
                if(menuButton.indexOf("删除") == -1){
                    $("[lay-event='del']").remove();
                }
            }
        });

        // 工具条点击事件
        table.on('tool(deviceTypeTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }else if(obj.event=='addSpec'){
                data.eventType='addSpec'
                showEditSpecModel(data);
            }else if(obj.event=='specTable'){
                showSpecTableModel(data);
            }else if(obj.event=='addAttr'){
                data.eventType='addAttr'
                showEditAttrModel(data);
            }else if(obj.event=='attrTable'){
                showAttrTableModel(data);
            }
        });

        form.on('switch(isDeleted)',function(obj){
            if(obj.elem.checked){
                enable(obj.value)
            }else{
                disable(obj.value)
            }
        })

        function enable(id){
            admin.req('enterprise/device/admin/deviceType/enable', {id:id}, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    table.reload('deviceTypeTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
        }

        function disable(id){
            admin.req('enterprise/device/admin/deviceType/disable', {id:id}, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    table.reload('deviceTypeTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
        }

        //监听排序
        table.on('sort(deviceTypeTable)', function(obj){
            table.reload('deviceTypeTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 添加按钮点击事件
        $('#deviceTypeBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '640px',
                offset: '65px',
                title: data ? '修改设备类型' : '添加设备类型',
                content: $('#deviceTypeForm').html(),
                success: function () {

                    form.val('deviceTypeForm', data);
                    form.render();
                    // 表单提交事件
                    form.on('submit(deviceTypeFormSubmit)', function (d) {
                        layer.load(2);

                        var isDeleted=$("form input[name='isDeleted']").prop("checked");
                        if(!isDeleted){
                            d.field.isDeleted=false;
                        }else{
                            d.field.isDeleted=true;
                        }
                        admin.req(data ? 'enterprise/device/admin/deviceType/update' : 'enterprise/device/admin/deviceType/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('deviceTypeTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        function showEditSpecModel(data) {
            var openIndex=admin.open({
                type: 1,
                area: '740px',
                offset: '65px',
                title: data&&(data.eventType=='addSpec'||data.eventType=='addSpecFromSpecTable') ? '添加设备规格值' : '修改设备规格值',
                content: $('#deviceSpecForm').html(),
                success: function () {
                    var deviceTypeName='';
                    if(data&&data.eventType=='editSpec'){
                        deviceTypeName=data.deviceTypeName;
                        data.specName=data.name;
                    }else{
                        deviceTypeName=data.name;
                    }


                    var option='<option selected>'+deviceTypeName+'</option>'
                    $("form select[name='productTypeId']").html(option);
                    form.render('select')
                    if(data&&data.eventType=='addSpec'){
                        data.sortNumber='';
                    }

                    form.val('deviceSpecForm', data);
                    form.render();

                    var getTemplateHtml=function(i){
                        var labelName='';
                        var valLen=$("form input[name='quantity']").length;
                        if(valLen==0){
                            labelName='规格值';
                        }
                        var templateHtml=''+
                            '<div class="layui-form-item" >' +
                            '<div class="layui-form-item">'+
                            '<div >'+
                            '<label class="layui-form-label" >'+labelName+'</label>' +
                            '<div class="layui-input-inline">' +
                            '<input id="value'+i+'" name="value" placeholder="请输规格值" type="text" class="layui-input" lay-verify="required" required/>' +
                            '</div>' +
                            '</div>' +

                        '<div >'+

                        '<div class="layui-input-inline">' +
                        '<input id="sort'+i+'" name="sort" placeholder="排序值" type="text" class="layui-input" lay-verify="sortNumber" required/>' +
                        '</div>' +
                        '</div>';

                            templateHtml+='<div class="layui-input-inline">' +
                                '<button id="btnDel'+i+'" class="layui-btn delBtn" lay-filter="btnDel'+i+'" style="margin-left: 20px;">删除</button>' +
                                '</div>';


                        templateHtml+='</div>' +

                            '</div>';

                        return templateHtml;
                    }

                    var i=0;
                    var btnCount=0;
                    var addTemplateDiv=function () {

                        var html=getTemplateHtml(i++);
                        $("#btnAddDiv").prev().after(html);

                        form.render();
                        // btnCount++;
                        form.render('select');

                        $(".delBtn").off('click').on('click',function(){
                            var btn=$(this);
                            var btnId=btn[0].id;
                            var btnNo=btnId.substring("btnDel".length,btnId.length);

                            btn.parent().parent().parent().remove();

                        });

                    };

                    $('#btnAdd').click(addTemplateDiv);

                    if(data==undefined||(data&&(data.eventType=='addSpec'||data.eventType=='addSpecFromSpecTable'))){
                        addTemplateDiv();
                        $("#btnDel0").hide();
                    }


                    if(data&&data.eventType=='editSpec'){
                        admin.req('enterprise/device/admin/deviceTypeSpecValue/list', {productSpecId:data.id,limit:9999}, function (res) {
                            if ( res.code==200) {
                                for(var k=0;k<res.data.length;k++){
                                    addTemplateDiv();
                                    $("#btnDel0").hide();
                                    var elementNo=i-1;
                                    var value=res.data[k].value;
                                    var sortNumber=res.data[k].sortNumber;
                                    $("#value"+elementNo).val(value);
                                    $("#sort"+elementNo).val(sortNumber);
                                    form.render();
                                }

                            } else {
                                layer.msg('获取规格值失败', {icon: 2, time: 1000});
                            }
                        }, 'GET');
                    }

                    // 表单提交事件
                    form.on('submit(deviceSpecFormSubmit)', function (d) {
                        layer.load(2);
                        var detailList=[];
                        var vals=[];
                        var sorts=[];
                        $("form input[name='value']").each(function(index,item){
                            var val=$(this).val();
                            vals.push(val);
                        });

                        $("form input[name='sort']").each(function(index,item){
                            var val=$(this).val();
                            sorts.push(val);
                        });


                        for(var k=0;k<vals.length;k++){
                            var obj={value:vals[k],sortNumber:sorts[k]};
                            detailList.push(obj)
                        }
                        if(data&&(data.eventType=='addSpec'||data.eventType=='addSpecFromSpecTable')){
                            d.field.id=undefined;
                            d.field.deviceType=data.id;
                        }else{
                            d.field.deviceType=data.deviceType;
                        }
                        d.field.name=d.field.specName;
                        d.field.detailList=detailList;
                        admin.req(data&&(data.eventType=='addSpec'||data.eventType=='addSpecFromSpecTable') ? 'enterprise/device/admin/deviceTypeSpec/add' : 'enterprise/device/admin/deviceTypeSpec/update', JSON.stringify(d.field), function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                if(data&&(data.eventType=='addSpecFromSpecTable'||data.eventType=='editSpec')){
                                    table.reload('deviceSpecTable');
                                    layer.close(openIndex);
                                }else{
                                    layer.closeAll('page');
                                }

                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'POST',true,'application/json; charset=utf-8');
                        return false;
                    });
                }
            });
        }
        //spec table
        function showSpecTableModel(data) {
            admin.open({
                type: 1,
                area: '1440px',
                offset: '65px',
                title: '设备规格管理',
                content: $('#specTable').html(),
                success: function () {
                    // 渲染表格
                    var insTb = table.render({
                        elem: '#deviceSpecTable',
                        url: config.base + 'enterprise/device/admin/deviceTypeSpec/list',
                        page: true,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'checkbox'}
                            ,{type: 'numbers',title:'序号'}
                            ,{field: 'name', sort: true, title: '规格名称'}
                            ,{field: 'deviceTypeName', sort: true, title: '设备类型'}
                            ,{field: 'specValues', sort: true, title: '规格值'}
                            ,{field: 'sortNumber', sort: true, title: '排序'}
                            ,{field: 'createTime', sort: true, title: '创建时间'}
                            ,{align: 'center', toolbar: '#deviceSpecTableBar', title: '操作', width: 200}
                        ]],
                        done: function (res, curr, count) {
                            //移除按钮
                            var menuButton = config.getMenuButton(location.hash);
                            if(menuButton.indexOf("搜索") == -1){
                                $('.layui-form.toolbar').remove();
                            }
                            if(menuButton.indexOf("修改") == -1){
                                $("[lay-event='edit']").remove();
                            }
                            if(menuButton.indexOf("删除") == -1){
                                $("[lay-event='del']").remove();
                            }
                        }
                    });

                    // 工具条点击事件
                    table.on('tool(deviceSpecTable)', function (obj) {
                        var data = obj.data;
                        if (obj.event === 'editSpec') { //修改
                            data.eventType='editSpec'
                            showEditSpecModel(data);
                        } else if (obj.event === 'del') { // 删除
                            doDelete(obj);
                        }
                    });

                    function doDelete(obj) {
                        layer.confirm('确定要删除吗？', {
                            offset: '65px',
                            skin: 'layui-layer-admin'
                        }, function (i) {
                            layer.close(i);
                            layer.load(2);
                            admin.req('enterprise/device/admin/deviceTypeSpec/' + obj.data.id, {}, function (res) {
                                layer.closeAll('loading');
                                if (res.code == 200) {
                                    layer.msg(res.msg, {icon: 1});
                                    obj.del();
                                    table.reload('deviceSpecTable');
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }, 'DELETE');
                        });
                    }

                    //监听排序
                    table.on('sort(deviceSpecTable)', function(obj){
                        table.reload('deviceSpecTable', {
                            initSort: obj
                            ,where: {
                                sort: obj.field
                                ,order: obj.type
                            }
                        });
                    });

                    // 搜索
                    form.on('submit(formSubSearchLog)', function (data) {
                        insTb.reload({where: data.field,page: {curr: 1}},'data');
                    });

                    // 添加按钮点击事件
                    $('#productSpecBtnAdd').click(function () {
                        data.eventType='addSpecFromSpecTable'
                        showEditSpecModel(data);
                    });

                    $('#productSpecBtnDel').click(function () {
                        var checkRows = table.checkStatus('deviceSpecTable');
                        if (checkRows.data.length == 0) {
                            layer.msg('请选择要删除的数据', {icon: 2});
                        } else {
                            var ids='';
                            checkRows.data.forEach(item=>{
                                ids+=item.id+','
                            })
                            delBatch(ids)
                        }
                    });

                    function delBatch(ids){
                        admin.req('enterprise/device/admin/deviceTypeSpec/delBatch', {ids:ids}, function (res) {
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('deviceSpecTable');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'POST');
                    }
                }
            })
        }
        //attr form
        function showEditAttrModel(data) {
            var openIndex=admin.open({
                type: 1,
                area: '740px',
                offset: '65px',
                title: data&&(data.eventType=='addAttr'||data.eventType=='addAttrFromAttrTable') ? '添加设备属性' : '修改设备属性',
                content: $('#deviceAttrForm').html(),
                success: function () {


                    /*var treeData=[];
                    admin.req('enterprise/device/admin/deviceAttributeCategory/getTreeData', {limit:9999}, function (res) {
                        if ( res.code==200) {
                            treeData=res.data;
                        } else {
                            layer.msg('获取设备分类失败', {icon: 2, time: 1000});
                        }
                    }, 'GET',false);

                    var selectedData=function(treeData,targetId){
                        for(var k=0;k<treeData.length;k++){
                            var nodeId=treeData[k].value;
                            if(targetId==nodeId){
                                treeData[k].selected=true;
                                return;
                            }else{
                                if(treeData[k].children&&treeData[k].children!=''){
                                    selectedData(treeData[k].children,targetId);
                                }
                            }
                        }
                    }

                    if(data&&data.eventType=='editAttr'){
                        selectedData(treeData,data.categoryId);
                    }



                    xmSelect.render({
                        el: '#selectCategory',
                        model: {label: {type: 'text'}},
                        radio: true,
                        clickClose: true,
                        filterable: true,
                        tree: {
                            show: true,
                            strict: false,
                            expandedKeys: [-1],
                        },
                        tips: '顶级分类',
                        height: 'auto',
                        data() {

                            return treeData;
                        },
                        on: function (data) {
                            //被选中的select的id,如果没有 -1
                            let parentId = data.arr.length > 0 ? data.change[0].value : -1;
                            $("input[name='categoryId']").val(parentId);
                        },
                    });*/

                    //


                    var deviceTypeName='';
                    if(data&&data.eventType=='editAttr'){
                        deviceTypeName=data.deviceTypeName;
                        data.attrName=data.name;
                    }else{
                        deviceTypeName=data.name;
                    }


                    var option='<option selected>'+deviceTypeName+'</option>'
                    $("form select[name='productTypeId']").html(option);
                    form.render('select')
                    if(data&&data.eventType=='addAttr'){
                        data.sortNumber='';
                    }

                    form.val('deviceAttrForm', data);
                    form.render();

                    /*var ele=$("#selectCategory");
                    if(ele[0].children.length>0){
                        var eleChild=ele[0].children[0].children[0]
                        if(eleChild){
                            $(eleChild).attr('lay-verify','required');
                        }
                    }
                    form.render();*/

                    var getTemplateHtml=function(i){
                        var labelName='';
                        var valLen=$("form input[name='value']").length;
                        var classNew='';
                        if(valLen==0){
                            labelName='属性值';
                        }else{
                            classNew='newAdd'
                        }
                        var templateHtml=''+
                            '<div class="attributeDiv layui-form-item '+classNew+'" >' +
                            '<div class="layui-form-item">'+
                            '<div >'+
                            '<label class="layui-form-label" >'+labelName+'</label>' +
                            '<div class="layui-input-inline">' +
                            '<input id="value'+i+'" name="value" placeholder="请输入属性" type="text" class="layui-input" lay-verify="required" required/>' +
                            '</div>' +
                            '</div>' +

                            '<div >'+

                            // '<div class="layui-input-inline">' +
                            // '<input id="sort'+i+'" name="sort" placeholder="排序值" type="text" class="layui-input" lay-verify="sortNumber" required/>' +
                            // '</div>' +
                            '</div>';

                        templateHtml+='<div class="layui-input-inline">' +
                            '<button id="btnDel'+i+'" class="layui-btn delBtn" lay-filter="btnDel'+i+'" style="margin-left: 20px;">删除</button>' +
                            '</div>';


                        templateHtml+='</div>' +

                            '</div>';

                        return templateHtml;
                    }

                    var i=0;
                    var btnCount=0;
                    var addTemplateDiv=function () {

                        var html=getTemplateHtml(i++);
                        $("#btnAddDiv2").prev().after(html);

                        form.render();
                        // btnCount++;
                        form.render('select');

                        $(".delBtn").off('click').on('click',function(){
                            var btn=$(this);
                            var btnId=btn[0].id;
                            var btnNo=btnId.substring("btnDel".length,btnId.length);

                            btn.parent().parent().parent().remove();

                        });

                    };

                    $('#btnAdd2').click(addTemplateDiv);
                    if(data&&(data.eventType=='addAttr'||data.eventType=='addAttrFromAttrTable')){
                        $("#btnAdd2").hide();
                    }
                    form.on('select(style)',function(data){
                        if(data.value=='text'){
                            $("#btnAdd2").hide();
                            $(".attributeDiv").remove();
                        }else if(data.value=='single'){
                            $("#btnAdd2").show();
                        }
                    })
                    if(data&&data.eventType=='editAttr'){
                        if(data.style=='single'){
                            $("#btnAdd2").show();
                        }else{
                            $("#btnAdd2").hide();
                        }
                    }

                    /*if(data==undefined||(data&&(data.eventType=='addAttr'||data.eventType=='addAttrFromAttrTable'))){
                        addTemplateDiv();
                        $("#btnDel0").hide();
                    }*/


                    if(data&&data.eventType=='editAttr'){
                        admin.req('enterprise/device/admin/deviceTypeAttributeValue/list', {deviceAttributeId:data.id,limit:9999}, function (res) {
                            if ( res.code==200) {
                                for(var k=0;k<res.data.length;k++){
                                    addTemplateDiv();
                                    $("#btnDel0").hide();
                                    var elementNo=i-1;
                                    var value=res.data[k].value;
                                    $("#value"+elementNo).val(value);
                                    form.render();
                                }

                            } else {
                                layer.msg('获取属性值失败', {icon: 2, time: 1000});
                            }
                        }, 'GET');
                    }

                    // 表单提交事件
                    form.on('submit(deviceAttrFormSubmit)', function (d) {
                        layer.load(2);
                        var detailList=[];
                        var vals=[];
                        var sorts=[];
                        $("form input[name='value']").each(function(index,item){
                            var val=$(this).val();
                            vals.push(val);
                        });



                        for(var k=0;k<vals.length;k++){
                            var obj={value:vals[k]};
                            detailList.push(obj)
                        }
                        if(data&&(data.eventType=='addAttr'||data.eventType=='addAttrFromAttrTable')){
                            d.field.id=undefined;
                            d.field.deviceType=data.id;
                        }else{
                            d.field.deviceType=data.deviceType;
                        }
                        d.field.name=d.field.attrName;
                        d.field.detailList=detailList;
                        var isDeleted=$("form input[name='isDeleted']").prop("checked");
                        if(!isDeleted){
                            d.field.isDeleted=false;
                        }else{
                            d.field.isDeleted=true;
                        }
                        admin.req(data&&(data.eventType=='addAttr'||data.eventType=='addAttrFromAttrTable') ? 'enterprise/device/admin/deviceTypeAttribute/add' : 'enterprise/device/admin/deviceTypeAttribute/update', JSON.stringify(d.field), function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                if(data&&(data.eventType=='addAttrFromAttrTable'||data.eventType=='editAttr')){
                                    table.reload('deviceAttrTable');
                                    layer.close(openIndex);
                                }else{
                                    layer.closeAll('page');
                                }

                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'POST',true,'application/json; charset=utf-8');
                        return false;
                    });
                }
            });
        }


        //attr table
        function showAttrTableModel(data) {
            admin.open({
                type: 1,
                area: '1440px',
                offset: '65px',
                title: '设备属性管理',
                content: $('#attrTable').html(),
                success: function () {
                    // 渲染表格
                    var insTb = table.render({
                        elem: '#deviceAttrTable',
                        url: config.base + 'enterprise/device/admin/deviceTypeAttribute/list',
                        page: true,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'checkbox'}
                            ,{type: 'numbers',title:'序号'}
                            ,{field: 'name', sort: true, title: '属性名称'}
                            ,{field: 'attrHeader', sort: true, title: '属性头'}
                            ,{field: 'categoryName', sort: true, title: '属性分类'}
                            ,{field: 'deviceTypeName', sort: true, title: '设备类型'}
                            ,{field: 'style', sort: true, title: '属性类型',templet:function (d) {
                                    if(d.style=='text'){
                                        return '文本';
                                    }else if(d.style=='single'){
                                        return  '单选';
                                    }
                                }}
                            ,{field: 'attrValues', sort: true, title: '属性值'}
                            ,{field: 'sortNumber', sort: true, title: '排序'}
                            ,{field: 'isDeleted', sort: true, title: '是否启用',templet:function (d) {
                                    if(d.isDeleted){
                                        return '<input name="isDeleted" type="checkbox" lay-skin="switch" value="'+d.id+'" checked="'+d.isDeleted+'" lay-filter="isDeleted"  lay-text="启用|停用"/>';
                                    }else{
                                        return '<input name="isDeleted" type="checkbox" lay-skin="switch" value="'+d.id+'" lay-filter="isDeleted"   lay-text="启用|停用"/>';
                                    }

                                }}
                            ,{field: 'createTime', sort: true, title: '创建时间'}
                            ,{align: 'center', toolbar: '#deviceAttrTableBar', title: '操作', width: 200}
                        ]],
                        done: function (res, curr, count) {
                            //移除按钮
                            var menuButton = config.getMenuButton(location.hash);
                            if(menuButton.indexOf("搜索") == -1){
                                $('.layui-form.toolbar').remove();
                            }
                            if(menuButton.indexOf("修改") == -1){
                                $("[lay-event='edit']").remove();
                            }
                            if(menuButton.indexOf("删除") == -1){
                                $("[lay-event='del']").remove();
                            }
                        }
                    });

                    // 工具条点击事件
                    table.on('tool(deviceAttrTable)', function (obj) {
                        var data = obj.data;
                        if (obj.event === 'editAttr') { //修改
                            data.eventType='editAttr'
                            showEditAttrModel(data);
                        } else if (obj.event === 'del') { // 删除
                            doDelete(obj);
                        }
                    });

                    function doDelete(obj) {
                        layer.confirm('确定要删除吗？', {
                            offset: '65px',
                            skin: 'layui-layer-admin'
                        }, function (i) {
                            layer.close(i);
                            layer.load(2);
                            admin.req('enterprise/device/admin/deviceTypeAttribute/' + obj.data.id, {}, function (res) {
                                layer.closeAll('loading');
                                if (res.code == 200) {
                                    layer.msg(res.msg, {icon: 1});
                                    obj.del();
                                    table.reload('deviceAttrTable');
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }, 'DELETE');
                        });
                    }

                    //监听排序
                    table.on('sort(deviceAttrTable)', function(obj){
                        table.reload('deviceAttrTable', {
                            initSort: obj
                            ,where: {
                                sort: obj.field
                                ,order: obj.type
                            }
                        });
                    });

                    // 搜索
                    form.on('submit(formSubSearchLog)', function (data) {
                        insTb.reload({where: data.field,page: {curr: 1}},'data');
                    });

                    form.on('switch(isDeleted)',function(obj){
                        if(obj.elem.checked){
                            enable(obj.value)
                        }else{
                            disable(obj.value)
                        }
                    })

                    function enable(id){
                        admin.req('enterprise/device/admin/deviceTypeAttribute/enable', {id:id}, function (res) {
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('deviceAttrTable');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');
                    }

                    function disable(id){
                        admin.req('enterprise/device/admin/deviceTypeAttribute/disable', {id:id}, function (res) {
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('deviceAttrTable');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');
                    }

                    // 添加按钮点击事件
                    $('#productAttrBtnAdd').click(function () {
                        data.eventType='addAttrFromAttrTable'
                        showEditAttrModel(data);
                    });

                    $('#productAttrBtnDel').click(function () {
                        var checkRows = table.checkStatus('deviceAttrTable');
                        if (checkRows.data.length == 0) {
                            layer.msg('请选择要删除的数据', {icon: 2});
                        } else {
                            var ids='';
                            checkRows.data.forEach(item=>{
                                ids+=item.id+','
                            })
                            delBatch(ids)
                        }
                    });

                    function delBatch(ids){
                        admin.req('enterprise/device/admin/deviceTypeAttribute/delBatch', {ids:ids}, function (res) {
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('deviceAttrTable');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'POST');
                    }
                }
            })
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/device/admin/deviceType/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#productTypeBtnExport').click(function () {
            var checkRows = table.checkStatus('deviceTypeTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>